package fr.umlv.android.caveAvin.sqlite;

import java.util.ArrayList;

import fr.umlv.android.caveAVin.beans.BottleWineCellar;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class BottleWineCellarBDD extends MyProjectBDD{
	
	private static final long serialVersionUID = 1L;
	
	private static final String TABLE_BOTTLE_WINE_CELAR = "table_bottle_wine_celar";
	
	private static final String COL_ID_WINE_CELAR_FK = "Id_wine_celar_fk";
	
	private static final int NUM_COL_ID_WINE_CELAR_FK = 0;
	
	private static final String COL_ID_BOTTLE_FK = "Id_bottle_fk";
	
	private static final int NUM_COL_ID_BOTTLE_FK = 1;
	
	private static final String COL_DATE_ADD = "date_add";
	
	private static final int NUM_COL_DATE_ADD = 2;

	public BottleWineCellarBDD(Context context) {
		super(context);
	}
	
	public static long insertBottleWineCellar(int bottleID, int wineCellarID, String date){
		//Création d'un ContentValues (fonctionne comme une HashMap)
		ContentValues values = new ContentValues();
		//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
		values.put(COL_ID_WINE_CELAR_FK, wineCellarID);
		values.put(COL_ID_BOTTLE_FK, bottleID);
		values.put(COL_DATE_ADD, date);
		//on insère l'objet dans la BDD via le ContentValues
		return bdd.insert(TABLE_BOTTLE_WINE_CELAR, null, values);
	}
	
	public static ArrayList<BottleWineCellar> getAllBottleWineCellar () {
		Cursor c = bdd.query(TABLE_BOTTLE_WINE_CELAR ,new String[] {COL_ID_WINE_CELAR_FK, COL_ID_BOTTLE_FK, COL_DATE_ADD}
					,"", null, null, null, null);
		
		return cursorToBottleWineCellar(c);
	}
	
	public static ArrayList<BottleWineCellar> getBottleWineCellarByBottleID (int bottleID) {
		Cursor c = bdd.query(TABLE_BOTTLE_WINE_CELAR ,new String[] {COL_ID_WINE_CELAR_FK, COL_ID_BOTTLE_FK, COL_DATE_ADD}
					,COL_ID_BOTTLE_FK + " = " + bottleID, null, null, null, null);
		
		return cursorToBottleWineCellar(c);
	}
	
	public static ArrayList<BottleWineCellar> getBottleWineCellarByWineCellarID (int wineCellarID) {
		Cursor c = bdd.query(TABLE_BOTTLE_WINE_CELAR ,new String[] {COL_ID_WINE_CELAR_FK, COL_ID_BOTTLE_FK, COL_DATE_ADD}
					,COL_ID_WINE_CELAR_FK + " = " + wineCellarID, null, null, null, null);
		
		return cursorToBottleWineCellar(c);
	}


	private static ArrayList<BottleWineCellar> cursorToBottleWineCellar(Cursor c) {
		ArrayList<BottleWineCellar> list = new ArrayList<BottleWineCellar>();
		BottleWineCellar bottleWineCellar = null;
		if ( c == null || c.getCount() == 0)
			return list;
		for (int i=0;i< c.getCount(); i++) {
			c.moveToPosition(i);
			bottleWineCellar = new BottleWineCellar();
			bottleWineCellar.setBottleId(c.getInt(NUM_COL_ID_BOTTLE_FK));
			bottleWineCellar.setWineCellarId(c.getInt(NUM_COL_ID_WINE_CELAR_FK));
			bottleWineCellar.setDateAdd(c.getString(NUM_COL_DATE_ADD));
			
			list.add(bottleWineCellar);
		}
		c.close();
		return list;
	}

}
